package com.ali.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FileReadUtil {
    /**
     * 目录读取
     */
    public static List<Map<String, Object>> directoryRead(String filePath) {
        List<Map<String, Object>> result = new ArrayList<>();

        File file = new File(filePath);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File childFile : files) {
                Map<String, Object> childFileMap = new HashMap<>() {{
                    put(childFile.getName(), childFile.getPath());
                    put("isDirectory", childFile.isDirectory());
                }};
                result.add(childFileMap);
            }
        }

        return result;
    }

    /**
     * 文件读取 - Scanner
     */
    public static List<Object> scannerFileRead(String filePath) {
        List<Object> result = new ArrayList<>();

        try (FileReader fileReader = new FileReader(filePath); Scanner scanner = new Scanner(fileReader)) {
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                result.add(line);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return result;
    }

    /**
     * 文件读取 - Files.lines
     */
    public static List<Object> filesLinesFileRead(String filePath) {
        Path path = Paths.get(filePath);
        try (Stream<String> stream = Files.lines(path, StandardCharsets.UTF_8)) {
            return stream.collect(Collectors.toList());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 读取文件 - Files.readAllLines
     */
    public static List<String> filesReadAllLinesFileRead(String filePath) {
        Path path = Paths.get(filePath);
        try {
            return Files.readAllLines(path, StandardCharsets.UTF_8);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 读取文件 - Files.readString
     */
    public static String filesReadStringFileRead(String filePath) {
        Path path = Paths.get(filePath);
        try {
            return Files.readString(path, StandardCharsets.UTF_8);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 读取文件 - Files.readAllBytes
     */
    public static String readAllBytesFileRead(String filePath) {
        Path path = Paths.get(filePath);
        try {
            byte[] bytes = Files.readAllBytes(path);
            return new String(bytes, StandardCharsets.UTF_8);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 读取文件 - BufferedReader
     */
    public static List<String> bufferedReaderFileRead(String filePath) {
        List<String> result = new ArrayList<>();

        Path path = Paths.get(filePath);
        try (BufferedReader bufferedReader = Files.newBufferedReader(path)) {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                result.add(line);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return result;
    }
}